{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.         1.         0.         1.         0.22807018 0.60869565]\n",
      " [0.61111111 0.1509434  0.22222222 0.32432432 0.68421053 1.        ]\n",
      " [0.41666667 0.58490566 1.         0.         0.         0.10869565]\n",
      " [0.         0.         0.96296296 0.94594595 1.         0.        ]]\n",
      "正理想解= [1. 1. 1. 1. 1. 1.] 负理想解= [0. 0. 0. 0. 0. 0.]\n",
      "[[ 0.          0.         -1.          0.         -0.77192982 -0.39130435]\n",
      " [-0.38888889 -0.8490566  -0.77777778 -0.67567568 -0.31578947  0.        ]\n",
      " [-0.58333333 -0.41509434  0.         -1.         -1.         -0.89130435]\n",
      " [-1.         -1.         -0.03703704 -0.05405405  0.         -1.        ]]\n",
      "[1.32249565 1.42594901 1.81851712 1.73328982] [1.85000714 1.42089892 1.23593709 1.67991411]\n",
      "topsis的评价值为： [0.58313806 0.49911304 0.40463435 0.492181  ]\n",
      "\n",
      " [[0.         0.         1.         0.         0.77192982 0.39130435]\n",
      " [0.38888889 0.8490566  0.77777778 0.67567568 0.31578947 0.        ]\n",
      " [0.58333333 0.41509434 0.         1.         1.         0.89130435]\n",
      " [1.         1.         0.03703704 0.05405405 0.         1.        ]]\n",
      "0.0\n",
      "1.0\n",
      "关联系数= [[1.         1.         0.33333333 1.         0.39310345 0.56097561]\n",
      " [0.5625     0.37062937 0.39130435 0.42528736 0.61290323 1.        ]\n",
      " [0.46153846 0.54639175 1.         0.33333333 0.33333333 0.359375  ]\n",
      " [0.33333333 0.33333333 0.93103448 0.90243902 1.         0.33333333]] \n",
      "关联度= [0.71456873 0.56043738 0.50566198 0.63891225]\n"
     ]
    }
   ],
   "source": [
    "#9.1\n",
    "\n",
    "import numpy as np\n",
    "a = np.array([[8.1,255,12.6,13.2,76,5.4],\n",
    "              [6.7,210,13.2,10.7,102,7.2],\n",
    "              [6.0,233,15.3,9.5,63,3.1],\n",
    "              [4.5,202,15.2,13,120,2.6]])\n",
    "for j in range(0,6):#数据归一化处理\n",
    "    a[:,j] = (a[:,j]-min(a[:,j]))/(max(a[:,j])-min(a[:,j]))\n",
    "print(a)#查看数据的表现情况\n",
    "#topsis方法\n",
    "cplus = a.max(axis = 0)\n",
    "cminus = a.min(axis = 0)\n",
    "print('正理想解=',cplus,'负理想解=',cminus)\n",
    "print(a-cplus)#每一列都减去cplus\n",
    "d1 = np.linalg.norm(a-cplus,axis = 1)\n",
    "d2 = np.linalg.norm(a-cminus,axis = 1)\n",
    "print(d1,d2)#显示到正理想解和负理想解的距离\n",
    "f1 = d2/(d1+d2)\n",
    "print('topsis的评价值为：',f1)#最终经过综合评估，1号方案可能比较适合\n",
    "#灰色关联分析\n",
    "t = cplus-a#计算参考序列与每个序列的差\n",
    "print('\\n',t)\n",
    "mmin = t.min()#计算最小值\n",
    "print(mmin)\n",
    "mmax = t.max()#计算最大值\n",
    "print(mmax)\n",
    "rho = 0.5 #分辨系数\n",
    "xs = (mmin + rho*mmax)/(t+rho*mmax)#计算灰色关联系数\n",
    "f2 = xs.mean(axis = 1)#求每一行的均值\n",
    "print(\"关联系数=\",xs,\"\\n关联度=\",f2)#显示灰色关联系数与灰色关联度，可以看出依旧是方案1较为合适"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(20, 4)\n",
      "[[0.19215372 0.29712579 0.2212766  0.31343284]\n",
      " [0.04643715 0.25419933 0.18723404 0.18656716]\n",
      " [0.06084868 0.12467339 0.02978723 0.        ]\n",
      " [0.16092874 0.30421799 0.24680851 0.34328358]\n",
      " [0.08967174 0.31205674 0.24680851 0.14179104]\n",
      " [0.         0.14296379 0.10212766 0.04477612]\n",
      " [0.23538831 0.2941396  0.15319149 0.20149254]\n",
      " [0.0256205  0.12989922 0.         0.05223881]\n",
      " [1.         0.70474057 0.52340426 1.        ]\n",
      " [0.53002402 0.80328481 0.69361702 0.47761194]\n",
      " [0.51561249 0.90444196 0.74893617 0.57462687]\n",
      " [0.10408327 0.26539754 0.29787234 0.17164179]\n",
      " [0.16573259 0.44382232 0.40425532 0.1641791 ]\n",
      " [0.29143315 0.44830161 0.44255319 0.49253731]\n",
      " [0.06565252 0.38260545 0.29787234 0.20149254]\n",
      " [0.31545236 0.5714819  0.30638298 0.1119403 ]\n",
      " [0.12890312 1.         1.         0.41044776]\n",
      " [0.09367494 0.24636058 0.27234043 0.14925373]\n",
      " [0.97277822 0.50167973 0.27659574 0.43283582]\n",
      " [0.49719776 0.         0.35744681 0.47761194]]\n",
      "\n",
      "\n",
      "******************************\n",
      "TOPSIS分析\n",
      "正理想解为： [1. 1. 1. 1.] 负理想解为： [0. 0. 0. 0.]\n",
      "[1.49145311 1.66965932 1.89460146 1.47875862 1.61424394 1.85826222\n",
      " 1.56127152 1.89861583 0.56064394 0.79141273 0.69838238 1.58791314\n",
      " 1.43486725 1.17247747 1.54427286 1.38635776 1.05184676 1.62562454\n",
      " 1.0459699  1.39232682] [0.52192937 0.36964524 0.14189184 0.54516484 0.43178649 0.1813108\n",
      " 0.45386412 0.1423345  1.66451533 1.27883137 1.4053373  0.44660723\n",
      " 0.6440673  0.85108985 0.52917328 0.72972797 1.47820275 0.40732605\n",
      " 1.20906248 0.77658676]\n",
      "topsis的评价值为： [0.25923011 0.18126044 0.06967459 0.2693604  0.2110362  0.08889645\n",
      " 0.22522758 0.06973932 0.74804323 0.61772009 0.66802498 0.21951475\n",
      " 0.30980643 0.42058885 0.25521438 0.34484802 0.58425843 0.200362\n",
      " 0.53616191 0.35805334]\n",
      "企业效益最好的是第9家企业\n",
      "企业效益最差的是第3家企业\n",
      "\n",
      "\n",
      "******************************\n",
      "灰色关联分析\n",
      "关联系数= [[0.38230793 0.41567106 0.39101498 0.42138365]\n",
      " [0.34398237 0.40134831 0.3808752  0.38068182]\n",
      " [0.34742698 0.36355001 0.34008683 0.33333333]\n",
      " [0.37339312 0.41813641 0.39898132 0.43225806]\n",
      " [0.35452739 0.42089552 0.39898132 0.36813187]\n",
      " [0.33333333 0.36845001 0.35768645 0.34358974]\n",
      " [0.39537828 0.4146417  0.37124803 0.38505747]\n",
      " [0.33912571 0.36493666 0.33333333 0.34536082]\n",
      " [1.         0.62872565 0.51198257 1.        ]\n",
      " [0.51547668 0.71765336 0.62005277 0.48905109]\n",
      " [0.50793005 0.83954873 0.66572238 0.54032258]\n",
      " [0.35818755 0.40498866 0.4159292  0.37640449]\n",
      " [0.37473747 0.4734052  0.45631068 0.37430168]\n",
      " [0.41371315 0.47542147 0.47283702 0.4962963 ]\n",
      " [0.34859057 0.44746952 0.4159292  0.38505747]\n",
      " [0.42210206 0.53849246 0.41889483 0.36021505]\n",
      " [0.36467153 1.         1.         0.45890411]\n",
      " [0.35553658 0.39883877 0.40727903 0.37016575]\n",
      " [0.9483675  0.50084128 0.40869565 0.46853147]\n",
      " [0.49860279 0.33333333 0.43761639 0.48905109]] \n",
      "关联度= [0.4025944  0.37672193 0.34609929 0.40569223 0.38563403 0.35076488\n",
      " 0.39158137 0.34568913 0.78517706 0.58555848 0.63838094 0.38887748\n",
      " 0.41968876 0.46456699 0.39926169 0.4349261  0.70589391 0.38295503\n",
      " 0.58160898 0.4396509 ]\n",
      "企业效益最好的是第9家企业\n",
      "企业效益最差的是第8家企业\n",
      "\n",
      "\n",
      "******************************\n",
      "熵值法\n",
      "P=[[0.0469967  0.03915841 0.03556701 0.05065211]\n",
      " [0.04168733 0.03490608 0.0314433  0.04549591]\n",
      " [0.04221243 0.02207514 0.01237113 0.03791325]\n",
      " [0.04585898 0.03986097 0.03865979 0.05186533]\n",
      " [0.04326264 0.04063748 0.03865979 0.04367607]\n",
      " [0.03999533 0.023887   0.02113402 0.03973309]\n",
      " [0.04857201 0.03886259 0.02731959 0.04610252]\n",
      " [0.04092885 0.02259281 0.00876289 0.0400364 ]\n",
      " [0.07643163 0.07953705 0.07216495 0.07855626]\n",
      " [0.05930745 0.08929892 0.09278351 0.05732484]\n",
      " [0.05878234 0.09931963 0.09948454 0.06126782]\n",
      " [0.04378774 0.03601538 0.04484536 0.04488929]\n",
      " [0.04603401 0.05369028 0.05773196 0.04458599]\n",
      " [0.05061408 0.054134   0.06237113 0.05793145]\n",
      " [0.04238747 0.04762609 0.04484536 0.04610252]\n",
      " [0.05148925 0.06633634 0.04587629 0.04246285]\n",
      " [0.04469209 0.10878568 0.12989691 0.05459509]\n",
      " [0.0434085  0.03412957 0.04175258 0.04397938]\n",
      " [0.07543977 0.05942168 0.04226804 0.055505  ]\n",
      " [0.05811138 0.00972489 0.05206186 0.05732484]]\n",
      "e=[0.99338877 0.95577612 0.94715065 0.9944707 ]\n",
      "g = [0.00661123 0.04422388 0.05284935 0.0055293 ]\n",
      "w=[0.06053474 0.40492956 0.48390744 0.05062826]\n",
      "F = [0.0384769  0.03417706 0.01940016 0.04025056 0.03999321 0.02433219\n",
      " 0.03423115 0.01789351 0.07573199 0.08755082 0.09501902 0.04120804\n",
      " 0.05472167 0.05809919 0.04588622 0.05432813 0.1123781  0.03887877\n",
      " 0.05189226 0.03555103]\n",
      "企业效益最好的是第17家企业\n",
      "企业效益最差的是第8家企业\n"
     ]
    }
   ],
   "source": [
    "#9.2\n",
    "\n",
    "''''\n",
    "经过分析，按照多数比例，第9家公司的经济效益较好，第8家企业的经济效益较差\n",
    "'''\n",
    "import numpy as np\n",
    "b = np.array([[1.611,10.59,0.69,1.67],\n",
    "             [1.429,9.44,0.61,1.50],\n",
    "             [1.447,5.97,0.24,1.25],\n",
    "             [1.572,10.78,0.75,1.71],\n",
    "             [1.483,10.99,0.75,1.44],\n",
    "             [1.371,6.46,0.41,1.31],\n",
    "             [1.665,10.51,0.53,1.52],\n",
    "             [1.403,6.11,0.17,1.32],\n",
    "             [2.62,21.51,1.40,2.59],\n",
    "             [2.033,24.15,1.80,1.89],\n",
    "             [2.015,26.86,1.93,2.02],\n",
    "             [1.501,9.74,0.87,1.48],\n",
    "             [1.578,14.52,1.12,1.47],\n",
    "             [1.735,14.64,1.21,1.91],\n",
    "             [1.453,12.88,0.87,1.52],\n",
    "             [1.765,17.94,0.89,1.40],\n",
    "             [1.532,29.42,2.52,1.80],\n",
    "             [1.488,9.23,0.81,1.45],\n",
    "             [2.586,16.07,0.82,1.83],\n",
    "             [1.992,2.63,1.01,1.89]])\n",
    "print(b.shape)\n",
    "#topsis方法\n",
    "for j in range(0,4):#数据归一化处理\n",
    "    b[:,j] = (b[:,j]-min(b[:,j]))/(max(b[:,j])-min(b[:,j]))\n",
    "print(b)#查看数据的表现情况\n",
    "print('\\n')\n",
    "print('*'*30)\n",
    "print('TOPSIS分析')\n",
    "cplus2 = b.max(axis = 0)\n",
    "cminus2 = b.min(axis = 0)\n",
    "print('正理想解为：',cplus2,'负理想解为：',cminus2)\n",
    "d3 = np.linalg.norm(b-cplus2,axis = 1)\n",
    "d4 = np.linalg.norm(b-cminus2,axis = 1)\n",
    "print(d3,d4)#显示到正理想解和负理想解的距离\n",
    "f2 = d4/(d3+d4)\n",
    "print('topsis的评价值为：',f2)#\n",
    "print('企业效益最好的是第{}家企业'.format(f2.argmax()+1))\n",
    "print('企业效益最差的是第{}家企业'.format(f2.argmin()+1))\n",
    "print('\\n')\n",
    "print('*'*30)\n",
    "print('灰色关联分析')\n",
    "t = cplus2-b#计算参考序列与每个序列的差\n",
    "mmin = t.min()#计算最小值\n",
    "mmax = t.max()#计算最大值\n",
    "rho = 0.5 #分辨系数\n",
    "xs = (mmin + rho*mmax)/(t+rho*mmax)#计算灰色关联系数\n",
    "f3 = xs.mean(axis = 1)#求每一行的均值\n",
    "print(\"关联系数=\",xs,\"\\n关联度=\",f3)#显示灰色关联系数与灰色关联度，可以看出依旧是方案1较为合适\n",
    "print('企业效益最好的是第{}家企业'.format(f3.argmax()+1))\n",
    "print('企业效益最差的是第{}家企业'.format(f3.argmin()+1))\n",
    "print('\\n')\n",
    "print('*'*30)\n",
    "print('熵值法')\n",
    "b1 = np.array([[1.611,10.59,0.69,1.67],\n",
    "             [1.429,9.44,0.61,1.50],\n",
    "             [1.447,5.97,0.24,1.25],\n",
    "             [1.572,10.78,0.75,1.71],\n",
    "             [1.483,10.99,0.75,1.44],\n",
    "             [1.371,6.46,0.41,1.31],\n",
    "             [1.665,10.51,0.53,1.52],\n",
    "             [1.403,6.11,0.17,1.32],\n",
    "             [2.62,21.51,1.40,2.59],\n",
    "             [2.033,24.15,1.80,1.89],\n",
    "             [2.015,26.86,1.93,2.02],\n",
    "             [1.501,9.74,0.87,1.48],\n",
    "             [1.578,14.52,1.12,1.47],\n",
    "             [1.735,14.64,1.21,1.91],\n",
    "             [1.453,12.88,0.87,1.52],\n",
    "             [1.765,17.94,0.89,1.40],\n",
    "             [1.532,29.42,2.52,1.80],\n",
    "             [1.488,9.23,0.81,1.45],\n",
    "             [2.586,16.07,0.82,1.83],\n",
    "             [1.992,2.63,1.01,1.89]])\n",
    "n = b1.shape[0]\n",
    "s = b1.sum(axis = 0)#逐列求和\n",
    "P = (1/s)*b1#求特征比重矩阵\n",
    "e = -(P*np.log(P)).sum(axis=0)/np.log(n) #计算熵值\n",
    "g = 1-e#计算差异系数\n",
    "w = g/sum(g)#计算权重\n",
    "F = P @ w\n",
    "print(\"P={}\\ne={}\\ng = {}\\nw={}\\nF = {}\".format(P,e,g,w,F))\n",
    "print('企业效益最好的是第{}家企业'.format(F.argmax()+1))\n",
    "print('企业效益最差的是第{}家企业'.format(F.argmin()+1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}